package com.meihua.code.thread;


import java.util.HashMap;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @program: code
 * @description:
 * @author: meihua
 * @created: 2021/06/11 15:35
 */
public class ThreadTest {
    private static volatile int i=0;
    private static AtomicBoolean flag = new AtomicBoolean(false) ;

    private static Object obj = new Object();

    public static Long s1(){

        long s = System.currentTimeMillis();
        Thread thread3 = new Thread(() -> {
            while (i < 100000) {
                if (flag.compareAndSet(true,false)) {
                 //   System.out.println(++i + "++ " + Thread.currentThread().getName());
                    flag.set(false);
                }
            }
        });
        Thread thread4 = new Thread(() -> {
            while (i < 100000) {
                if (flag.compareAndSet(false,true)) {
                   // System.out.println(++i + "++ " + Thread.currentThread().getName());
                    flag.set(true);
                }
            }
        });
        thread4.start();
        thread3.start();
        try {
            thread3.join();
            thread4.join();
        }catch (Exception e){
        }
        long l = System.currentTimeMillis() - s;
        System.out.println("耗时 "+(l)+"ms");
        return l;
    }

    public static  long s2(){
        long l = System.currentTimeMillis();
        Thread thread2 = new Thread(() -> {
            while (true) {
                synchronized (obj) {
                    if (i<100000){
                        obj.notify();
                      //  System.out.println(++i + "-- " + Thread.currentThread().getName());
                        try {
                            obj.wait();
                        } catch (Exception e) {
                        }
                    }else {
                        obj.notify();
                        break;
                    }

                }
            }
        });

        Thread thread1 = new Thread(() -> {
            while (true) {
                synchronized (obj) {
                    if (i<100000){
                        obj.notify();
                      //  System.out.println(++i + "-- " + Thread.currentThread().getName());
                        try {
                            obj.wait();
                        } catch (Exception e) {
                        }
                    }else {
                        obj.notify();
                        break;
                    }
                }
            }
        });

        thread1.start();
        thread2.start();
        try {
            thread2.join();
            thread1.join();
        } catch (Exception e) {
        }
        long l1 = System.currentTimeMillis() - l;
        System.out.println("耗时 "+(l1)+"ms");
        return l1;
    }


    public static void main(String[] args) {

        HashMap<Long, Long> map = new HashMap<>(8);
        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 1000000; i++) {
                map.put( System.currentTimeMillis(), System.currentTimeMillis());
            }
        });
        Thread thread = new Thread(() -> {
            for (int i = 0; i < 1000000; i++) {
                map.put( System.currentTimeMillis(), System.currentTimeMillis());
            }
        });
        thread.start();
        thread1.start();

        try {
            thread.join();
            thread1.join();
            System.out.println(map);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        long sum = 0;
//        for (int j=0;j<10;j++){
//            sum +=  s2();
//            i=0;
//        }
//        System.out.println("总耗时 "+sum+"ms 平均耗时："+sum/10+" ms");
//
//        for (int j=0;j<10;j++){
//            sum +=  s1();
//            i=0;
//        }
//        System.out.println("总耗时 "+sum+"ms 平均耗时："+sum/10+" ms");



    }


}
